package com.bai.edu.controller;


import com.bai.edu.entity.Course;
import com.bai.edu.entity.Teacher;
import com.bai.edu.entity.vo.CourseInfoVo;
import com.bai.edu.entity.vo.CoursePublishVo;
import com.bai.edu.service.CourseService;
import com.bai.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author bai
 * @since 2023-07-25
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class CourseController {
    @Autowired
    private CourseService courseService;

    //TODO:课程列表
    @GetMapping("/{current}/{limit}")
    public R getCourseList(@PathVariable long current,
                           @PathVariable long limit){
        Page<Course> coursePage = new Page<>(current,limit);
        courseService.page(coursePage,null);
        long total = coursePage.getTotal();
        List<Course> records = coursePage.getRecords();
        Map map = new HashMap();
        map.put("total", total);
        map.put("records", records);
        return R.ok().data(map);
    }


    //添加课程
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.saveCourseInfo(courseInfoVo);
        return R.ok();
    }

    //根据id查询课程信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId){
      CourseInfoVo courseInfoVo=  courseService.getCourseInfoById(courseId);
      return R.ok().data("courseInfoVo",courseInfoVo);
    }

    //修改课程信息
    @PostMapping("updateCourse")
    public R update(@RequestBody CourseInfoVo courseInfoVo){
        courseService.updateCourseInfoVo(courseInfoVo);
        return R.ok();
    }

    @GetMapping("getPublishCourse/{courseId}")
    public R publishCourseInfo(@PathVariable String courseId){
       CoursePublishVo coursePublishVo= courseService.publishCourseInfo(courseId);
       return R.ok().data("publishCourse",coursePublishVo);
    }

    //课程最终发布
    @PostMapping("publishCourse/{courseId}")
    public R publishCourse(@PathVariable String courseId){
        Course course = new Course();
        course.setId(courseId);
        course.setStatus("Normal");
        courseService.updateById(course);
        return R.ok();
    }

    //删除课程
    @DeleteMapping("{courseId}")
    public R deleteCourseById(@PathVariable String courseId){
        courseService.removeCourse(courseId);
        return R.ok();
    }
}

